//
//  WithdrawViewController.m
//  PPDL
//
//  Created by 王洋 on 2019/7/25.
//  Copyright © 2019 王洋. All rights reserved.
//

#import "BankCardModel.h"
#import "WithdrawViewController.h"
#import "BankCardPopViewController.h"
#import "AddBankCardViewController.h"
#import "MyBankCardViewController.h"

@interface WithdrawViewController ()


@property (nonatomic, strong) BankCardModel *selectBankCardModel;

@property (weak, nonatomic) IBOutlet UIButton *bankCardBtn;

@property (weak, nonatomic) IBOutlet UITextField *moneyTF;

@property (weak, nonatomic) IBOutlet UILabel *allMoneyLab;

@property (nonatomic, assign) BOOL isWait;


@end

@implementation WithdrawViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self initView];
    [self prepareData];
}

#pragma mark 申请提现

- (void)request_WithDrawApply {
    
    self.isWait = YES;
    NSDictionary *dic = @{}.mutableCopy;
    [dic setValue:self.amount forKey:@"amount"];
    [dic setValue:@"2" forKey:@"clientType"];// 客户端类型 1.微信小程序 2.APP 5.公众号
    [dic setValue:@"0" forKey:@"type"];// 转账方式 0银行卡打款 1微信转账 2支付宝转账
    [dic setValue:self.selectBankCardModel.ID forKey:@"bankId"];
    
    NSString *url = [NSString stringWithFormat:@"%@%@",TT_Global_Api_Domain,TT_MEMBER_WITHDRAW_APPLY_V2];
    
    [TTHttpManager postRequestWithUrl:url TimeOut:10 PostData:dic FinishBlock:^(id  _Nonnull data) {
        
        self.isWait = NO;
        if ([data isKindOfClass:[NSDictionary class]]) {
            
            NSString * code = [data valueForKey:@"code"];
            if ([code isEqualToString:@"200"]) {
                
                [self.navigationController popToRootViewControllerAnimated:YES];
                [self alert:[data valueForKey:@"msg"]];
                
            } else {
                
                [self alert:[data valueForKey:@"msg"]];
            }
            
        } else {
            
        }
        
        
    } FailedBlock:^(NSError * _Nonnull error) {
        self.isWait = NO;

    }];
}


//- (void)request_WithDrawApply {
//
//    NSDictionary *dic = @{}.mutableCopy;
//    [dic setValue:self.moneyTF.text forKey:@"amount"];
//
//    NSString *bankId =self.selectBankCardModel.ID;
//    [dic setValue:bankId forKey:@"bankId"];
//
//    NSString *url = [NSString stringWithFormat:@"%@%@",TT_Global_Api_Domain,TT_MEMBER_WITHDRAW_APPLY];
//
//    [TTHttpManager postRequestWithUrl:url TimeOut:10 PostData:dic FinishBlock:^(id  _Nonnull data) {
//
//        if ([data isKindOfClass:[NSDictionary class]]) {
//
//            NSString * code = [data valueForKey:@"code"];
//            if ([code isEqualToString:@"200"]) {
//
//                [SVProgressHUD showSuccessWithStatus:[data valueForKey:@"msg"]];
//                [SVProgressHUD dismissWithDelay:1];
//                [self.navigationController popViewControllerAnimated:YES];
//
//            } else {
//
//                [SVProgressHUD showErrorWithStatus:[data valueForKey:@"msg"]];
//                [SVProgressHUD dismissWithDelay:1];
//            }
//
//        } else {
//
//        }
//
//
//    } FailedBlock:^(NSError * _Nonnull error) {
//
//    }];
//
//
//}

- (void)prepareData {
    
    if (self.selectBankCardModel.ID.length) {
        
    } else {
        
        [self.bankCardBtn setTitle:@"请选择银行卡" forState:UIControlStateNormal];
    }
}

//全部提现
- (IBAction)cashOutClicked:(id)sender {

    self.moneyTF.text = [NSString stringWithFormat:@"%.2f",self.model.itemWithDrawMoney];
}

//添加或改变银行卡
- (IBAction)addOrChangeCard:(id)sender {
    
    MyBankCardViewController *VC = [[MyBankCardViewController alloc] init];
    WEAKSELF
    VC.changeBankCard = ^(BankCardModel * _Nonnull model) {
        
        weakSelf.selectBankCardModel = model;
        [weakSelf.bankCardBtn setTitle:[NSString stringWithFormat:@"%@(%@)",model.bankName,model.bankCardID] forState:UIControlStateNormal];

    };
    [self.navigationController pushViewController:VC animated:YES];
    
//    NSArray *payTypeArr = @[@{@"pic":@"pic_alipay",
//                              @"title":@"支付宝",
//                              @"type":@"alipay"},
//                            @{@"pic":@"pic_wxpay",
//                              @"title":@"微信",
//                              @"type":@"wxpay"},
//                            @{@"pic":@"pic_blance",
//                              @"title":@"余额",
//                              @"type":@"balance"}];
    
//    BankCardPopViewController *pop = [[BankCardPopViewController alloc]initDefaultIndex:0 defaultHeight:250.0f vc:self dataSource:self.bankCardArray];
//    STPopupController *popVericodeController = [[STPopupController alloc] initWithRootViewController:pop];
//    popVericodeController.style = STPopupStyleBottomSheet;
//    [popVericodeController presentInViewController:self];
//
//    WEAKSELF
//    pop.selectIndex = ^(NSInteger index) {
//
//        if (index < self.bankCardArray.count) {
//            //选中银行卡
//
//            //移除掉所有默认，并且将选中的额设为默认
//            for (int i = 0; i<self.bankCardArray.count; i++) {
//
//                if (i != index) {
//                    self.bankCardArray[i].isDefault = NO;
//                } else {
//                    self.bankCardArray[i].isDefault = YES;
//                    [self.bankCardBtn setTitle:self.bankCardArray[i].bankName forState:UIControlStateNormal];
//                }
//            }
//
//
//
//        } else {
//            //添加银行卡
//            AddBankCardViewController *addCard = [[AddBankCardViewController alloc] init];
//            addCard.vcTitle = @"添加银行卡";
//            [weakSelf.navigationController pushViewController:addCard animated:YES];
//        }
//
//    };
    
}

//提现按钮
- (IBAction)withDraw:(id)sender {
    
    if (!self.selectBankCardModel.ID) {
        [self alert:@"请选择银行卡"];
        return;
    }
    
    if (!self.isWait) {
        [self request_WithDrawApply];
    }
}

- (void)initView {
    
    self.isWait = NO;
    
    self.title = @"银行卡提现";
    self.allMoneyLab.text =[NSString stringWithFormat:@"您可提现金额¥ %@",[Utilities changeDoubleToString:self.model.itemWithDrawMoney]];
}

- (BankCardModel *)selectBankCardModel {
    
    if (!_selectBankCardModel) {
        _selectBankCardModel = [[BankCardModel alloc] init];
    }
    return _selectBankCardModel;
}

@end
